
#include <BFont>
#include <BStyle>
#include <BWidget>
#include "member_BLineEdit.h"

using namespace BWE;

member_BLineEdit::member_BLineEdit(BLineEdit* edit)
{
	boss = edit;
	align = Align_LeftCenter;
	readOnly = false;
	blinkTime = 1.0;
	blinkTick = 0.0;

	dirty = true;
	editing = false;
	pressed = false;
	hoverIndex = 0;
	inputShown = false;
}
member_BLineEdit::~member_BLineEdit()
{

}

bool member_BLineEdit::freshSizes()
{
	const BStyle* style = boss->realStyle();
	if (style)
	{
		style->translate(suffix, t_suffix);
		style->translate(prefix, t_prefix);
	}
	const BFont* font = boss->font();
	if (!font)
		return false;
	prefixSize.reset();
	if (prefix.size())
	{
		if (t_prefix.size())
			prefixSize = font->textSize(t_prefix);
		else
			prefixSize = font->textSize(prefix);
	}
	suffixSize.reset();
	if (suffix.size())
	{
		if (t_suffix.size())
			suffixSize = font->textSize(t_suffix);
		else
			suffixSize = font->textSize(suffix);
	}
	return true;
}
void member_BLineEdit::freshRects()
{
	const BFont* font = boss->font();
	if (!font)
		return;

	if (dirtySize)
	{
		freshSizes();
		dirtySize = false;
	}

	BRect crect = boss->centerRect();
	textArea = crect;
	textArea.left() += prefixSize.width();
	textArea.right() -= suffixSize.width();

	if (block.dirtyCells || block.dirtyFont)
	{
		block.adjust(0);
	}
	BRect textRect = textArea.align(align, block.textSize);
	block.rect = textRect;

	BRect prefixArea(crect.left(), crect.top(), prefixSize.width(), crect.height());
	prefixRect = prefixArea.align(Align_Center, prefixSize);

	BRect suffixArea(block.rect.right(), crect.top(), suffixSize.width(), crect.height());
	suffixRect = suffixArea.align(Align_Center, suffixSize);

	dirtyLine = true;
}
void member_BLineEdit::freshInputLine()
{
	if (boss->focused())
	{
		if (block.cells.check(block.inputIndex))
		{
			BRect cellRect = block.cells[block.inputIndex].rect + block.rect.pos();
			lineBegin.set(cellRect.left(), block.rect.top());
			lineEnd.set(cellRect.left(), block.rect.bottom());

		}
		else
		{
			BRect cellRect = block.cells.last().rect + block.rect.pos();
			lineBegin.set(cellRect.right(), block.rect.top());
			lineEnd.set(cellRect.right(), block.rect.bottom());
		}
		if (!textArea.contain(lineBegin))
		{
			BPoint point = textArea.wrap(lineBegin);
			BPoint offset = point - lineBegin;
			block.rect.move(offset.x(), 0);
			lineBegin.x() = point.x();
			lineEnd.x() = point.x();
		}
	}
}
